package com.lsh.oms.provider.service.order.ka;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.order.OrderDeliveryKADTO;
import com.lsh.oms.api.service.order.ka.IKaOrderDeliveryRestService;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.provider.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Project Name: lsh-oms
 *
 * @author miaozhuang
 * @date 17/3/27
 * 北京链商电子商务有限公司
 * Package
 * desc:订单发货RestService
 */
@Service(protocol = "rest", timeout = 30000)
@Path("order/delivery/ka")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({ContentType.APPLICATION_JSON_UTF_8})
@Deprecated
public class KaOrderDeliveryRestServiceImpl extends BaseService implements IKaOrderDeliveryRestService {

    private static Logger logger = LoggerFactory.getLogger(KaOrderDeliveryRestServiceImpl.class);

//    @Value("${shopUrl}")
//    private String shopUrl;

//    @Value("#{'${big.ka.dcs}'.split(',')}")
//    private List<String> dcs;

    @POST
    @Path("create")
    @Override
    public CommonResult<Object> create(OrderDeliveryKADTO orderDeliveryKADTO) {
//        String logSign = "logSign wg ka: " + ToolService.getLogSign();
//        long startTimeMs = System.currentTimeMillis();

//        orderDeliveryKADTO.setVenderId(this.getVenderId());
//        logger.info(logSign + ":【文固发货入参】 －－ " + (orderDeliveryKADTO == null ? "" : JsonUtils.obj2Json(orderDeliveryKADTO)));
//
        CommonResult<Object> commonResult = new CommonResult<>();
//        try {
//            if (orderDeliveryKADTO == null) {
//                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "请求参数不合法，请检验。");
//            }
//
//            if (orderDeliveryKADTO.getOrderCode() == null) {
//                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "请求参数不合法，请检验。");
//            }
//
//            OrderHead orderHead = this.orderQueryService.findByCode(orderDeliveryKADTO.getOrderCode(), false, false);
//            if (null == orderHead) {
//                logger.info(logSign + "请求耗时:" + (startTimeMs - System.currentTimeMillis()) + "ms");
//                throw new BusinessException(ReturnCode.FIND_ORDER_IS_NULL, "用户订单号未查询到订单");
//            }
//
//            Integer orderStatus = orderHead.getOrderStatus();
//            if (orderStatus.compareTo(OrderStatus.ORDER_COMMITING_OFC.getIndex()) < 0) {
//                throw new BusinessException(ReturnCode.ORDER_STATUS_ERROR, "订单状态是" + OrderStatus.getName(orderHead.getOrderStatus()) + "，请检验。");
//            }
//
//            if (orderStatus >= OrderStatus.ORDER_DELIVERY.getIndex()) {
//
//                if (orderStatus == OrderStatus.ORDER_DELIVERY.getIndex() || orderStatus == OrderStatus.ORDER_SIGN.getIndex()) {
//                    OrderHead orderHead4receipt = orderQueryService.findByCode(orderHead.getOrderCode(), true, false);
//                    //签收发生在发货以后
//                    if (orderHead4receipt.getOrderStatus().compareTo(OrderStatus.ORDER_DELIVERY.getIndex()) < 0) {
//                        throw new BusinessException(ReturnCode.ORDER_STATUS_ERROR, "订单状态是" + OrderStatus.getName(orderHead.getOrderStatus()) + "，请检验。");
//                    }
//                    commonResult = orderReceiptKAService.createReceiptOrder(orderHead4receipt, logSign);
//                    this.asyncNotifyShop(commonResult, orderHead);
//                    return commonResult;
//                }
//
//                throw new BusinessException(ReturnCode.REQUEST_FAIL, "发货失败！订单状态为`" + OrderStatus.getName(orderStatus) + "`，请核实！");
//            }
//
//            OrderShippingHeadKaDto orderShippingHeadKaDto = orderDeliveryKADTO.getOrderShippingHead();
//            if (orderShippingHeadKaDto == null || orderShippingHeadKaDto.getShippingDetails() == null) {
//                throw new BusinessException(ReturnCode.FIND_ORDER_IS_NULL, "orderShippingKAHeadDto 必传");
//            }
//
////            commonResult = this.orderDeliveryKAService.createShippingOrder(orderShippingHeadKaDto, orderHead, logSign);
//
//            logger.info("发货完成结果信息 {}", JSON.toJSONString(commonResult));
//            if (!commonResult.getCode().equals(ReturnCode.REQUEST_SUCCESS)) {
//                throw new BusinessException(ReturnCode.ORDER_STATUS_ERROR, "订单发货失败");
//            }
//
//            OrderHead orderHead4receipt = this.orderQueryService.findByCode(orderHead.getOrderCode(), true, false);
//            //签收发生在发货以后
//            if (orderHead4receipt.getOrderStatus().compareTo(OrderStatus.ORDER_DELIVERY.getIndex()) < 0) {
//                throw new BusinessException(ReturnCode.ORDER_STATUS_ERROR, "订单状态是" + OrderStatus.getName(orderHead.getOrderStatus()) + "，请检验。");
//            }
//
//            commonResult = this.orderReceiptKAService.createReceiptOrder(orderHead4receipt, logSign);
//            this.asyncNotifyShop(commonResult, orderHead);
//        } catch (BusinessException e) {
//            commonResult.setCode(e.getCode());
//            commonResult.setMessage(e.getMessage());
//            logger.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
//        } catch (Exception e) {
//            commonResult.setCode(ExceptionStatus.E3001001.getCode());
//            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
//            logger.error("业务异常code : " + ExceptionStatus.E3001001.getCode() + ",异常信息: " + e.getMessage(), e);
//        }

        return commonResult;
    }

//    /**
//     * 以销定采-订单签收，通知商城（开发票）
//     *
//     * @param commonResult
//     */
//    private void asyncNotifyShop(CommonResult commonResult, OrderHead orderHead) {
//        if (commonResult.getCode().equals(ReturnCode.REQUEST_SUCCESS) && commonResult.getData() instanceof OrderSignHead) {
//            OrderSignHead oshHead = (OrderSignHead) commonResult.getData();
//            JSONObject extJson = JSON.parseObject(orderHead.getExt());
//            String orderDc = extJson.getString("orderDc");
////            if (dcs.contains(orderHead.getRegionCode() + "-" + orderDc)) {
//////            if (Integer.valueOf(Zone.BEIJING_XFD.getZoneCode()).equals(orderHead.getRegionCode()) && dcs.contains(orderDc)) {
////                logger.info("以销定采，订单签收，通知商城，orderCode is {}", oshHead.getOrderId());
////                ReceiptOrderDone receiptOrderDone = new ReceiptOrderDone();
////                BeanUtils.copyProperties(oshHead, receiptOrderDone);
////                receiptOrderDone.setArrivedAt(oshHead.getCreatedAt());
////                OmsEventBus.post(receiptOrderDone);
////            }
//        }
//    }

    @POST
    @Path("/create/confirm")
    @Override
    public CommonResult<Integer> confirm(long code) {

        return new CommonResult<>(ReturnCode.REQUEST_SUCCESS, "ok", 1);
    }
}
